package jude.java.money.provide.impl;

import java.util.ArrayList;
import java.util.List;

import jude.java.money.beans.WeakPassword;
import jude.java.money.provide.BackablePagingProvider;
import jude.java.money.spring.Services;

import org.apache.commons.lang.StringUtils;

public class WeakPasswordProvider extends BackablePagingProvider<String> {

	private int[] levels;
	private String userName;
	
	public WeakPasswordProvider(int... levels) {
		super(200);
		this.levels = levels;
		setHisrotySize(1);
	}

	@Override
	protected List<String> loadRecords(int pageSize, int pageIndex) {
		List<WeakPassword> ps = Services.getScanService().listWeakPasswords(pageSize, pageIndex, levels);
		if (ps == null || ps.isEmpty())
			return null;
		ArrayList<String> r = new ArrayList<String>();
		boolean u = StringUtils.isNotEmpty(userName);
		if (u)
			r.add(userName);
		for (WeakPassword weakPassword : ps) {
			r.add(weakPassword.getPassword());
			if (u)
				r.add(userName + weakPassword.getPassword());
		}
		return r;
	}
	
	public synchronized void reset(String userName) {
		super.reset();
		this.userName = userName;
	}
	
	@Override
	public void back() {
		super.back();
	}
}
